package cqu.zhaiyixin20215521.huarondao.slice.pictureSelect;

import cqu.zhaiyixin20215521.huarondao.ResourceTable;
import cqu.zhaiyixin20215521.huarondao.slice.pintuInitialize.Pintu;
import cqu.zhaiyixin20215521.huarondao.slice.pintuInitialize.PintuInitializeAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;
import ohos.media.image.PixelMap;

public class PictureSelectAbilitySlice extends AbilitySlice {
    PixelMap pintuPicture = null;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_picture_select);
        initListContainer(); //初始化图片选择列表
        initRowColPicker(); //初始化行数列数选择器
        setGridRowCol(2, 2); //初始化小方块矩阵
        initSubmitButton(); //初始化提交按钮
    }

    private void initListContainer() {
        ListContainer picturesList = findComponentById(ResourceTable.Id_PictureSelectAbilityPicturesList);
        Component.ClickedListener clickedListener = component -> {
            setPintuPicture(((Image)component).getPixelMap());
        };
        //展示的图片
        int[] ImageResourceIDs = new int[] {ResourceTable.Media_cqu_classroom, ResourceTable.Media_cqu_dangwei, ResourceTable.Media_cqu_gate, ResourceTable.Media_cqu_lake, ResourceTable.Media_cqu_library, ResourceTable.Media_cqu_tower};
        ImageListProvider imageListProvider = new ImageListProvider(this, ImageResourceIDs, clickedListener);
        picturesList.setItemProvider(imageListProvider);
    }

    private void initRowColPicker() {
        Picker rowNumPicker = findComponentById(ResourceTable.Id_PictureSelectAbilityRowNumPicker);
        Picker colNumPicker = findComponentById(ResourceTable.Id_PictureSelectAbilityColNumPicker);
        rowNumPicker.setMinValue(2);
        rowNumPicker.setValue(2);
        rowNumPicker.setMaxValue(3);
        colNumPicker.setMinValue(2);
        colNumPicker.setValue(2);
        colNumPicker.setMaxValue(3);
        rowNumPicker.setValueChangedListener((picker, i, i1) -> {
            setGridRowCol(rowNumPicker.getValue(), colNumPicker.getValue());
        });
        colNumPicker.setValueChangedListener((picker, i, i1) -> {
            setGridRowCol(rowNumPicker.getValue(), colNumPicker.getValue());
        });
    }

    private void setPintuPicture(PixelMap pixelMap) {
        this.pintuPicture = pixelMap;
        TableLayout pintuGrid = findComponentById(ResourceTable.Id_PictureSelectAbilityPiecesTable);
        if(pixelMap == null) {
            for(int i = 0; i < pintuGrid.getChildCount(); ++i) {
                Image image = (Image) pintuGrid.getComponentAt(i);
                image.setPixelMap(null);
            }
        } else {
            PixelMap cropedPixelMap = PicProcessor.crop(pixelMap, pintuGrid.getRowCount() /(double) pintuGrid.getColumnCount());
            PixelMap[] pieces = PicProcessor.cutUp(cropedPixelMap, pintuGrid.getRowCount(), pintuGrid.getColumnCount());
            for(int i = 0; i < pieces.length; ++i) {
                Image image = (Image) pintuGrid.getComponentAt(i);
                image.setPixelMap(PicProcessor.drawText(PicProcessor.drawBorder(pieces[i]), ""+(i+1)));
            }
        }
    }

    private void setGridRowCol(int rowNum, int colNum) {
        TableLayout pintuGrid = findComponentById(ResourceTable.Id_PictureSelectAbilityPiecesTable);
        DirectionalLayout frame = findComponentById(ResourceTable.Id_PictureSelectAbilityTableFrame);
        ComponentContainer.LayoutConfig layoutConfig = pintuGrid.getLayoutConfig();
        if(rowNum >= colNum) { //设置TableLayout的长宽
            layoutConfig.height = frame.getHeight();
            layoutConfig.width = frame.getHeight() * colNum / rowNum;
        } else {
            layoutConfig.width = frame.getWidth();
            layoutConfig.height = frame.getWidth() * rowNum / colNum;
        }
        pintuGrid.setLayoutConfig(layoutConfig);
        pintuGrid.removeAllComponents();
        pintuGrid.setRowCount(rowNum);
        pintuGrid.setColumnCount(colNum);
        for(int i = 0; i < rowNum; ++i) { //初始化TableLayout里的Image组件
            for(int j = 0; j < colNum; ++j) {
                Image image = new Image(this);
                image.setScaleMode(Image.ScaleMode.STRETCH);
                pintuGrid.addComponent(image, layoutConfig.width / colNum, layoutConfig.height / rowNum);
            }
        }
        setPintuPicture(pintuPicture);
    }

    private void initSubmitButton() {
        Button button = findComponentById(ResourceTable.Id_PictureSelectAbilitySubmitButton);
        TableLayout pintuGrid = findComponentById(ResourceTable.Id_PictureSelectAbilityPiecesTable);
        button.setClickedListener(component -> {
            if(pintuPicture == null) {
                new ToastDialog(this)
                        .setText("请选择一张图片!")
                        .show();
            } else {
                PixelMap[] pixelMaps = new PixelMap[pintuGrid.getRowCount() * pintuGrid.getColumnCount()];
                for(int i = 0; i < pixelMaps.length; ++i) {
                    Image image = (Image)pintuGrid.getComponentAt(i);
                    pixelMaps[i] = image.getPixelMap();
                }
                IntentParams params = new IntentParams();
                params.setParam("rowCount", pintuGrid.getRowCount());
                params.setParam("colCount", pintuGrid.getColumnCount());
                params.setParam("pixelMaps", pixelMaps);
                params.setParam("picture", PicProcessor.crop(pintuPicture, pintuGrid.getRowCount() /(double) pintuGrid.getColumnCount()));
                Intent intent = new Intent();
                intent.setParams(params);
                present(new PintuInitializeAbilitySlice(), intent);
            }
        });
    }
}
